/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package warmachineroster.impl;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;

import warmachineroster.RosterElement;
import warmachineroster.UnitRosterElement;
import warmachineroster.WarmachineRosterPackage;
import warmachinesystem.Unit;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Unit Roster Element</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link warmachineroster.impl.UnitRosterElementImpl#getUnitAttachment <em>Unit Attachment</em>}</li>
 *   <li>{@link warmachineroster.impl.UnitRosterElementImpl#getModelCount <em>Model Count</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class UnitRosterElementImpl extends RosterElementImpl implements UnitRosterElement {
	/**
	 * The cached value of the '{@link #getUnitAttachment() <em>Unit Attachment</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getUnitAttachment()
	 * @generated
	 * @ordered
	 */
	protected EList<RosterElement> unitAttachment;

	/**
	 * The default value of the '{@link #getModelCount() <em>Model Count</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getModelCount()
	 * @generated
	 * @ordered
	 */
	protected static final int MODEL_COUNT_EDEFAULT = 0;

	/**
	 * The cached value of the '{@link #getModelCount() <em>Model Count</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getModelCount()
	 * @generated
	 * @ordered
	 */
	protected int modelCount = MODEL_COUNT_EDEFAULT;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected UnitRosterElementImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return WarmachineRosterPackage.Literals.UNIT_ROSTER_ELEMENT;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<RosterElement> getUnitAttachment() {
		if (unitAttachment == null) {
			unitAttachment = new EObjectContainmentEList<RosterElement>(RosterElement.class, this, WarmachineRosterPackage.UNIT_ROSTER_ELEMENT__UNIT_ATTACHMENT);
		}
		return unitAttachment;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public int getModelCount() {
		return modelCount;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setModelCount(int newModelCount) {
		int oldModelCount = modelCount;
		modelCount = newModelCount;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, WarmachineRosterPackage.UNIT_ROSTER_ELEMENT__MODEL_COUNT, oldModelCount, modelCount));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case WarmachineRosterPackage.UNIT_ROSTER_ELEMENT__UNIT_ATTACHMENT:
				return ((InternalEList<?>)getUnitAttachment()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	@Override
	public int getPointCost() {
		
		int baseUnitCost = ((Unit)getUnitType()).getPointCost();
		int baseModelCount = ((Unit)getUnitType()).getBaseModelNumber();
		
		int pointCost = baseUnitCost;
		
		int increment = ((Unit)getUnitType()).getIncrement();
		
		int incrementNumber = 0;
		
		if (increment > 0)
			incrementNumber = (getModelCount() - baseModelCount) / increment;
		

		pointCost += incrementNumber *((Unit)getUnitType()).getIncrementCost();
		
//		for (RosterElement re : getUnitAttachment()){
//			pointCost+= re.getPointCost();
//		}
		
		return pointCost;
	}
	
	/**
	 * il pointValue di una Unit è uguale al suo pointCost più il pointValue di tutti gli attachment
	 * @generated NOT
	 */
	@Override
	public int getPointValue() {
		int pointValue = getPointCost();
		for (RosterElement re: getUnitAttachment()){
			pointValue += re.getPointValue();
		}
		return pointValue;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case WarmachineRosterPackage.UNIT_ROSTER_ELEMENT__UNIT_ATTACHMENT:
				return getUnitAttachment();
			case WarmachineRosterPackage.UNIT_ROSTER_ELEMENT__MODEL_COUNT:
				return getModelCount();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case WarmachineRosterPackage.UNIT_ROSTER_ELEMENT__UNIT_ATTACHMENT:
				getUnitAttachment().clear();
				getUnitAttachment().addAll((Collection<? extends RosterElement>)newValue);
				return;
			case WarmachineRosterPackage.UNIT_ROSTER_ELEMENT__MODEL_COUNT:
				setModelCount((Integer)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case WarmachineRosterPackage.UNIT_ROSTER_ELEMENT__UNIT_ATTACHMENT:
				getUnitAttachment().clear();
				return;
			case WarmachineRosterPackage.UNIT_ROSTER_ELEMENT__MODEL_COUNT:
				setModelCount(MODEL_COUNT_EDEFAULT);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case WarmachineRosterPackage.UNIT_ROSTER_ELEMENT__UNIT_ATTACHMENT:
				return unitAttachment != null && !unitAttachment.isEmpty();
			case WarmachineRosterPackage.UNIT_ROSTER_ELEMENT__MODEL_COUNT:
				return modelCount != MODEL_COUNT_EDEFAULT;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (modelCount: ");
		result.append(modelCount);
		result.append(')');
		return result.toString();
	}

} //UnitRosterElementImpl
